/**
 * Copyright 2016-2018 mobaas.com
 */
package com.mobaas.devops.controller;/*
 * @Auther: hua
 * @Date: 2018/7/31
 */

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.mobaas.devops.model.VcsDayStat;
import com.mobaas.devops.model.VcsMonthStat;
import com.mobaas.devops.service.VcsStatService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * @author billy zhang
 * 
 */
@Controller
@RequestMapping(value = "/dev/vcs")
public class DevVcsController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DevVcsController.class);

    @Autowired
    VcsStatService vcsService;
    
    private Date getToday() {
    	Calendar cal = Calendar.getInstance();
    	cal.set(Calendar.HOUR_OF_DAY, 0);
    	cal.set(Calendar.MINUTE, 0);
    	cal.set(Calendar.SECOND, 0);
    	cal.set(Calendar.MILLISECOND, 0);
    	
    	return cal.getTime();
    }

	private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy-MM");
	
    private Date parseDate(String dateStr, Date defaultDate) {
    	try {
			return fmt.parse(dateStr);
		} catch (ParseException e) {
			return defaultDate;
		}
    }
    
    @GetMapping("/daystat")
    public  ModelAndView dayStat(
    		@RequestParam(value="num", required=false, defaultValue="30")int num,
    		@RequestParam(value="date", required=false, defaultValue="")String date){

	    	Date endDate = getToday();
	    	Date beginDate = DateUtils.addDays(endDate, 0 - num);

        ModelAndView mv = new ModelAndView();
        
        List<VcsDayStat> list = vcsService.selectDayStatList(beginDate, endDate);

        SimpleDateFormat dtFmt = new SimpleDateFormat("MM-dd");
		List<String> dayList = list.stream().map(td -> "'" + dtFmt.format(td.getStatDate()) + "'").collect(Collectors.toList());
		List<Integer> totalList = list.stream().map(td -> td.getFileTotal()).collect(Collectors.toList());

		Date currDate = parseDate(date, getToday());
		
		List<VcsDayStat> statsList = vcsService.selectDayStatListByDate(currDate);
		
		mv.addObject("days", StringUtils.join(dayList, ","));
		mv.addObject("totals", StringUtils.join(totalList, ","));
		
		mv.addObject("statslist", statsList);
		mv.addObject("date", currDate);
		
        mv.setViewName("/dev/vcs/daystat");
        return mv;
    }
    
    @GetMapping("/monthstat")
    public ModelAndView monthStat(
    		@RequestParam(value="num", required=false, defaultValue="6")int num,
    		@RequestParam(value="month", required=false, defaultValue="")String month) {

	    	ModelAndView mv = new ModelAndView();
	    	
	    	List<VcsMonthStat> list = vcsService.selectMonthStatList(num);
	
	    	List<String> dayList = list.stream().map(td -> "'" + td.getStatMonth() + "'").collect(Collectors.toList());
		List<String> totalList = list.stream().map(td -> String.format("{value:%d,name:'%s'}", td.getFileTotal(), td.getStatMonth()) ).collect(Collectors.toList());

		mv.addObject("months", StringUtils.join(dayList, ","));
		mv.addObject("totals", StringUtils.join(totalList, ","));
	
		if (StringUtils.isEmpty(month)) {
			month = fmt2.format(new Date());
		}
		List<VcsMonthStat> statsList = vcsService.selectMonthStatListByMonth(month);
		
	    	mv.addObject("statslist", statsList);
	    	mv.addObject("month", month);
	    	
	    	mv.setViewName("/dev/vcs/monthstat");
	    	return mv;
    }
    
}
